#ifndef _BROKERIMPL_H_
#define _BROKERIMPL_H_

#include "stdafx.h"
#include "node.h"

class CBrokerImpl : public	IBroker,
					public  IOperationEvent,
					public	IAcceptListen,
					public	ITimerEvent,
					public	SChannel,
					public	CUnknownImp
		
{
public:
	CBrokerImpl(void);
	virtual ~CBrokerImpl(void);

	BEGIN_STDCOM_MAP
		STDCOM_INTERFACE_ENTRY_UNKNOWN_(IBroker)
		STDCOM_INTERFACE_ENTRY(IOperationEvent)
		STDCOM_INTERFACE_ENTRY(IAcceptListen)
		STDCOM_INTERFACE_ENTRY(ITimerEvent)
		STDCOM_INTERFACE_ENTRY(IBroker)
	END_STDCOM_MAP

public:

	//IBroker
	std_method(Listen)(LPCSTR addr, int len, int maxconn);
	std_method(Dispatch)(unsigned int sec);
	std_method(Close)();
	std_method(IsListen)();

	//IAcceptListen
	std_method(Accept)(IBase* pBase, _sock_t s, event_id error_code);

	//IOperationEvent
	std_method(OnComplete)(IBase *pOperation, event_id event, ULONG error_code);

	//ITimerEvents
	std_method(OnTimer)(UINT uId, UINT err);
private:
	std_method(OnAccepted)(IBase* pBase, _sock_t s);
	std_method(OnRecvSeqed)(IBase *pIBase);
	std_method(OnSendSeqAcked)(IBase *pIBase);
	std_method(OnRecvAcked)(IBase *pIBase);
	std_method(OnSended)(IBase *pIBase);
	std_method(OnRecved)(IBase *pIBase);
private: 
	std_method(Init)();
	std_method(UnInit)();
	std_method(PushQueue)(IBase* pBase);

private:
	CLockQueue<Node*>		m_AcceptQueue;
	CLockQueue<Node*>		m_ConnectQueue;
	CLockQueue<Node*>		m_Consumer;
	CLockQueue<Node*>		m_Producer;

	UINT					m_MaxConn;

	CSemHandle				m_sem;
	CAutoLock				m_lock;
private:
	TimerPtr				m_ConnectTimer;
	TimerPtr				m_HeartTimer;

};

#endif
